package com.wkk.service.impl;

import com.wkk.entity.ConsoleInfo;
import com.wkk.entity.Income;
import com.wkk.entity.IncomeOneDay;
import com.wkk.entity.SignIn;
import com.wkk.mapper.AdminMapper;
import com.wkk.service.AdminService;
import com.wkk.util.GeneralCalculation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class AdminServiceImpl implements AdminService {

    @Autowired
    private AdminMapper adminMapper;


    @Override
    public IncomeOneDay getIncomeByATime(String date) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        GeneralCalculation generalCalculation = new GeneralCalculation();
        Map<String,Object> map = new HashMap<>();
        if(date.equals("notUpload")){
            //如果没有传递日期信息，则默认为当天的日期
            String time = simpleDateFormat.format(new Date());
            IncomeOneDay incomeOneDay = new IncomeOneDay();
            List<Income> incomes = adminMapper.selectListIncomeByDateInTypeTotal(time);
            generalCalculation.assignmentIncome(incomes,incomeOneDay);//赋值操作
            return incomeOneDay;
        }else{
            String[] time=date.split("-");
            IncomeOneDay incomeOneDay = new IncomeOneDay();
            if(time.length==1){//date="2020"
                map.put("startTime",date+"-01-01");//该年第一天
                map.put("endTime",date+"-12-31");//该年最后一天
            }else if (time.length==2){//date="2020-2"
                int day = generalCalculation.getDayOfMonth(Integer.parseInt(time[0]),Integer.parseInt(time[1]));
                map.put("startTime",date+"-01");//该月第一天
                map.put("endTime",date+"-"+day);//该月最后一天
            }else if (time.length==3){//date="2020-4-4"
                List<Income> incomes = adminMapper.selectListIncomeByDateInTypeTotal(date);
                generalCalculation.assignmentIncome(incomes,incomeOneDay);//赋值操作
                return incomeOneDay;
            }
            //用于解析length==1和length==2
            List<Income> incomes = adminMapper.selectListIncomeByATime(map);
            generalCalculation.assignmentIncome(incomes,incomeOneDay);
            return incomeOneDay;
        }
    }

    @Override
    public boolean userSignIn(SignIn signIn) {
        List<SignIn> inList = adminMapper.selectListSignIn();
        for(SignIn sign:inList){
            if(sign.getDate().equals(signIn.getDate())&&sign.getUid()==signIn.getUid()){
                //判断该用户是否已经签到,若是已经签到则返回false
                return false;
            }
        }
        //用户签到成功
        adminMapper.insertOneSignIn(signIn);
        return true;

    }

    @Override
    public ConsoleInfo getConsoleInfo() {
        Calendar calendar = Calendar.getInstance();//用于获取日期
        GeneralCalculation generalCalculation = new GeneralCalculation();//用于调用自定义工具方法
        ConsoleInfo consoleInfo = new ConsoleInfo();
        List<String> pastSevenDate =  generalCalculation.getLastDays(8);//获取出发点是今天的过去八天的日期字符串
        Map<String,Object> map = new HashMap<>();
        map.put("startTime",generalCalculation.getFirstDayOfMonth(calendar.get(Calendar.MONTH)));//本月的第一天
        map.put("endTime",generalCalculation.getLastDayOfMonth(calendar.get(Calendar.MONTH)));//本月的最后一天


        consoleInfo.setMonthTotalIncome(adminMapper.monthIncomeSum(map));//**将本月的总收入赋值
        List<Integer> pastSevenDateTotalIncome = new ArrayList<>();//创建过去七日变化--收入
        generalCalculation.initList(pastSevenDateTotalIncome);
        List<Integer> pastSevenDateSignInCount = new ArrayList<>();//创建过去七日变化--用户签到量
        generalCalculation.initList(pastSevenDateSignInCount);
        //将昨天的收入赋值
        IncomeOneDay incomeOneDay = new IncomeOneDay();
        incomeOneDay.setTime(pastSevenDate.get(1));
        List<Income> incomes = adminMapper.selectListIncomeByDateInTypeTotal(pastSevenDate.get(1));
        generalCalculation.assignmentIncome(incomes,incomeOneDay);
        consoleInfo.setYesterdayInCome(incomeOneDay);//**将昨日收益赋值
        List<Integer> pastSevenDateMemberCard = new ArrayList<>();//创建过去七日变化--会员卡收入
        generalCalculation.initList(pastSevenDateMemberCard);
        List<Integer> pastSevenDatePrivateLesson = new ArrayList<>();//创建过去七日变化--私教课收入
        generalCalculation.initList(pastSevenDatePrivateLesson);
        List<Integer> pastSevenDateFeaturedLesson = new ArrayList<>();//创建过去七日变化--特色课收入
        generalCalculation.initList(pastSevenDateFeaturedLesson);
        List<Integer> pastSevenDateRehabilitationLesson = new ArrayList<>();//创建过去七日变化--康复课收入
        generalCalculation.initList(pastSevenDateRehabilitationLesson);
        List<Integer> pastSevenDateAdvertisement = new ArrayList<>();//创建过去七日变化--广告收入
        generalCalculation.initList(pastSevenDateAdvertisement);



        int size = pastSevenDate.size();
        //将每天的增长趋势赋值给七日变化的list中
        for(int i=1;i<size;i++){
            //该操作实现:通过第七天的日期信息,获取当天的总收入
            pastSevenDateTotalIncome.set(i-1,adminMapper.dayIncomeSum(pastSevenDate.get(size-i)));
            //获取每天用户的签到数
            pastSevenDateSignInCount.set(i-1,adminMapper.selectListSignInByDate(pastSevenDate.get(size-i)).size());

            //获取每天的收入
            //List<Income> incomeList = adminMapper.selectListIncomeByDate(pastSevenDate.get(size-i));

            List<Income> incomeList = adminMapper.selectListIncomeByDateInTypeTotal(pastSevenDate.get(size-i));
           /* List<Income> incomeList1 = new ArrayList<>();
            for(Income income:incomeList){
                if(incomeList1.size()>0){
                    for(Income income1:incomeList1){

                    }
                }else{
                    incomeList1.add(income);
                }
            }*/

            for(Income income:incomeList){
                //通过if判断一次分类赋值
                if(income.getType().equals("广告收入")){
                    pastSevenDateAdvertisement.set(i-1,income.getMoney());
                }
                if(income.getType().equals("私教课收入")){
                    pastSevenDatePrivateLesson.set(i-1,income.getMoney());
                }
                if(income.getType().equals("会员卡收入")){
                    pastSevenDateMemberCard.set(i-1,income.getMoney());
                }
                if(income.getType().equals("特色课收入")){
                    pastSevenDateFeaturedLesson.set(i-1,income.getMoney());
                }
                if(income.getType().equals("康复课收入")){
                    pastSevenDateRehabilitationLesson.set(i-1,income.getMoney());
                }

            }

        }
        consoleInfo.setPastSevenDateMemberCard(pastSevenDateMemberCard);//**过去七天的会员卡收入
        consoleInfo.setPastSevenDatePrivateLesson(pastSevenDatePrivateLesson);//**过去七天的私教课收入
        consoleInfo.setPastSevenDateFeaturedLesson(pastSevenDateFeaturedLesson);//**过去七天的特色课收入
        consoleInfo.setPastSevenDateRehabilitationLesson(pastSevenDateRehabilitationLesson);//**过去七天的康复课收入
        consoleInfo.setPastSevenDateAdvertisement(pastSevenDateAdvertisement);//**过去七天的广告收入

        consoleInfo.setPastSevenDateTotalIncome(pastSevenDateTotalIncome);//**将过去七天的总收入赋值
        consoleInfo.setPastSevenDateSignInCount(pastSevenDateSignInCount);//**将过去七天的签到数赋值

        //覆盖先前的值【本人失误,之前没有把date与time分开导致】
        map.put("startTime",generalCalculation.getFirstDayOfMonth(calendar.get(Calendar.MONTH))+" 00:00:00");//本月的第一天
        map.put("endTime",generalCalculation.getLastDayOfMonth(calendar.get(Calendar.MONTH))+" 23:59:59");//本月的最后一天

        //System.out.println(generalCalculation.getFirstDayOfMonth(calendar.get(Calendar.MONTH))+" 00:00:00"+"~~~"+generalCalculation.getLastDayOfMonth(calendar.get(Calendar.MONTH)+1)+" 23:59:59");

        consoleInfo.setMonthNewAddMember(adminMapper.getMemberOrUserCountByStartToEndTime(map));//**将本月新增的会员数量赋值
        consoleInfo.setMonthNewAddChat(adminMapper.getChatCountByStartToEndTime(map));//**将本月新增的朋友圈数量赋值

        List<Integer> pastSevenDateNewAddMember = new ArrayList<>();//创建过去七日变化--新增会员量
        generalCalculation.initList(pastSevenDateNewAddMember);
        List<Integer> pastSevenDateNewAddChat = new ArrayList<>();//创建过去七日变化--新增朋友圈量
        generalCalculation.initList(pastSevenDateNewAddChat);
        map.put("user","123");//用于实现动态sql中的方法,其值没实际意义
        //在这里加入是为了时间被下面的循环覆盖
        consoleInfo.setMonthNewAddUser(adminMapper.getMemberOrUserCountByStartToEndTime(map));//**将本月新增的用户数量赋值
        map.remove("user");
        for(int i =1;i<size;i++){
            String startTime = pastSevenDate.get(size-i)+" 00:00:00";
            String endTime = pastSevenDate.get(size-i)+" 23:59:59";
            map.put("startTime",startTime);//当天的零时零分
            map.put("endTime",endTime);//当天的最后一分最后一秒
            pastSevenDateNewAddChat.set(i-1,adminMapper.getChatCountByStartToEndTime(map));
            pastSevenDateNewAddMember.set(i-1,adminMapper.getMemberOrUserCountByStartToEndTime(map));
        }
        consoleInfo.setPastSevenDateNewAddChat(pastSevenDateNewAddChat);//**将过去七天的新增朋友圈数赋值
        consoleInfo.setPastSevenDateNewAddMember(pastSevenDateNewAddMember); //**将过去七天的会员赋值

        List<Integer> pastSevenDateNewAddUser = new ArrayList<>();//创建过去七日变化--新增用户量
        generalCalculation.initList(pastSevenDateNewAddUser);

        map.put("user","123");//用于实现动态sql中的方法,其值没实际意义
        for(int i = 1;i<size;i++){
            String startTime = pastSevenDate.get(size-i)+" 00:00:00";
            String endTime = pastSevenDate.get(size-i)+" 23:59:59";
            map.put("startTime",startTime);//当天的零时零分
            map.put("endTime",endTime);//当天的最后一分最后一秒
           // System.out.println(startTime+"~~"+endTime);
            pastSevenDateNewAddUser.set(i-1,adminMapper.getMemberOrUserCountByStartToEndTime(map));
        }
        consoleInfo.setPastSevenDateNewAddUser(pastSevenDateNewAddUser);//**将过去七天的用户数量增长
        return consoleInfo;
    }
}
